//
//  String+Extension.swift
//  Sevengrass
//
//  Created by Syrena on 2017/12/11.
//  Copyright © 2017年 Syrena. All rights reserved.
//

import Foundation

extension String {

    var minToHour: String {
        let min = self.int.or(0)
        if min < 60 {
            return "\(min)分"
        } else if min % 60 > 0 {
            return "\(min / 60)小时\(min % 60)分钟"
        } else {
            return "\(min / 60)小时"
        }
    }

    var url:URL {
        guard self != "" else {
            return URL(string: "-")!
        }
        return URL(string: self)!
    }

    var hostUrl: URL {
        guard self != "" else {
            return URL(string: "-")!
        }
        return URL(string: baseURL + self)!
    }

    // MARK: - 计算字符串长度
    var lengthWhenCountingNonASCIICharacterAsTwo: Int {

        var i = 0
        for cha in self.unicodeScalars {
            if cha.isASCII {
                i += 1
            } else {
                i += 2
            }
        }
        return i

    }

    // MARK: - 智能计算文字长度对应的文字提示时间
    var smartDelaySecondsForToastText: Double {
        let length = self.lengthWhenCountingNonASCIICharacterAsTwo
        switch length {
        case ...20:
            return 1.5
        case 21...40:
            return 2.0
        case 41...50:
            return 2.5
        default:
            return 3.0
        }
    }

    func transformToPinYin() -> String {

        let mutableString = NSMutableString(string: self)
        //把汉字转为拼音
        CFStringTransform(mutableString, nil, kCFStringTransformToLatin, false)
        //去掉拼音的音标
        CFStringTransform(mutableString, nil, kCFStringTransformStripDiacritics, false)

        let string = String(mutableString)
        //去掉空格
        return string.replacingOccurrences(of: " ", with: "")
    }

    // MARK: - 时间转字符串
    func stringToDate() -> Date {

        let format = DateFormatter.init()
        format.dateFormat = "yyyy-MM-dd HH:mm"
        return format.date(from: self)!
    }

    func isInt() -> Bool {
        let scanner = Scanner(string: self)
        var val:Int = 0
        return scanner.scanInt(&val) && scanner.isAtEnd
    }

    func getStringSize(font:UIFont) -> CGSize {
        return  NSString(string:self).size(withAttributes: [NSAttributedStringKey.font:font])
    }

    public var image:UIImage {
        return UIImage.init(named: self)!
    }

    // MARK: - 将字符串里面所有等于传入字符串的文字替换为相应样式
    public func changeText(_ targetText:String,color:UIColor,font:UIFont?) -> NSAttributedString {
        let attr = NSMutableAttributedString(string:self)
        for index in 0..<self.count {
            guard let rangeStr = self[safe:index..<(targetText.count + index)],rangeStr == targetText else {
                continue
            }
            if (font != nil) {
                attr.addAttributes([NSAttributedStringKey.foregroundColor : color,NSAttributedStringKey.font
                    : font!], range: NSRange(location: index, length: targetText.count))
            } else {
                attr.addAttribute(NSAttributedStringKey.foregroundColor, value: color, range: NSRange(location: index, length: targetText.count))
            }
        }
        return attr
    }

    public func replacing(range: CountableClosedRange<Int>, with replacementCharacter: Character) -> String {
        guard range.count <= self.count - 4 else {
            return self
        }
        let start = index(startIndex, offsetBy: range.lowerBound)
        let end   = index(start, offsetBy: range.count - 1)
        return self.replacingCharacters(in: start ..< end, with: String.init(repeating: replacementCharacter, count: range.count - 1))
    }

}

infix operator ..+
public func ..+ <Bound: Strideable>(lhs: Bound, rhs: Bound.Stride) -> CountableClosedRange<Bound> {
    return lhs ... lhs.advanced(by: rhs)
}
